//
//  JXLCircleAnimator.m
//  自定义专场动画
//
//  Created by 贾晓磊 on 16/10/27.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

#import "JXLCircleAnimator.h"
@interface JXLCircleAnimator () <UIViewControllerAnimatedTransitioning,CAAnimationDelegate>

@end

@implementation JXLCircleAnimator {
    //是否是展示
    BOOL _isPresented;
    //转场动画上下文
   __weak id <UIViewControllerContextTransitioning> _transitionContext;
}
//负责告诉控制器谁来提供转场动画
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    _isPresented = YES;
    return self;
}
//负责告诉控制器谁来解除转场动画
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    _isPresented = NO;
    return self;
}

#pragma mark UIViewControllerAnimatedTransitioning

/**
 返回转场动画时长

 @param transitionContext transitionContext 转场上下文

 @return 返回动画时长
 */
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    return 1;
}

/**
 转场动画最核心的方法，由程序员提供自己的动画实现

 @param transitionContext transitionContext 转场上下文 提供转场动画的所有细节
 containerView 是转场动画实现的舞台
 转场上下文会对展现的控制器强引用
 */
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    //1，获取容器视图
    UIView *containerView = [transitionContext containerView];
    
    //2，获取目标视图，如果是展现取 toView  ／ 如果是解除取 fromView
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    UIView *view = _isPresented? toView : fromView;
    //NSLog(@"%@",[view subviews]);
    
    if (_isPresented) {
        //3，添加目标视图到容器视图
        [containerView addSubview:view];
    }
    
    //4，针对view做动画
    [self circleAnimationWithView:view];
    
    //纪录转场动画上下文
    _transitionContext = transitionContext;
//    //5，一定要完成 如果不完成，系统会一直等待转场完成，就无法接受用户的任何交互
//    [transitionContext completeTransition:YES];
    
}


#pragma mark 转场动画代理方法
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
        //5，一定要完成 如果不完成，系统会一直等待转场完成，就无法接受用户的任何交互
        [_transitionContext completeTransition:YES];
    NSLog(@"%@",anim);
}
#pragma mark 转场动画
- (void)circleAnimationWithView:(UIView *)view {
    //1，实例化shapeLayer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
    // 2，设置图层属性
    // 路径
    CGFloat radius = 30;
    CGFloat rightMargin = 16;
    CGFloat topMargin = 28;
    CGFloat viewWidth = view.bounds.size.width;
    CGFloat viewHeight = view.bounds.size.height;
    
    CGRect beginRect = CGRectMake(viewWidth - radius - rightMargin, topMargin, radius, radius);
    
    UIBezierPath *beginPath = [UIBezierPath bezierPathWithOvalInRect:beginRect];
    
    shapeLayer.path = beginPath.CGPath;
    
    view.layer.mask = shapeLayer;
    
    CGFloat maxRadius = sqrt(viewWidth * viewWidth + viewHeight *viewHeight);
    
    // 结束位置 - 利用缩进，参数为负，是放大矩形，中心点保持不变
    CGRect endRect = CGRectInset(beginRect, -maxRadius, -maxRadius);
    
    UIBezierPath *endPath = [UIBezierPath bezierPathWithOvalInRect:endRect];
    
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"path"];
    //设置动画代理
    anim.delegate = self;
    
    //判断是否是展现
    if (_isPresented) {
        anim.fromValue = (__bridge id _Nullable)(beginPath.CGPath);
        anim.toValue = (__bridge id _Nullable)(endPath.CGPath);
    } else {
        anim.fromValue = (__bridge id _Nullable)(endPath.CGPath);
        anim.toValue = (__bridge id _Nullable)(beginPath.CGPath);
    }
    
    //设置动画时长
    anim.duration = [self transitionDuration:_transitionContext];
    
    anim.fillMode = kCAFillModeForwards;
    
    anim.removedOnCompletion = NO;
    [shapeLayer addAnimation:anim forKey:nil];
    
//    //设置动画代理，如果将动画添加到图层，动画会立即开始，然后再设置代理，无效！
//    anim.delegate = self;
    
}

@end
